package com.atguigu.auth.activiti;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;

/**
 * @version 1.0
 * @autor 雷记新
 */
@SpringBootTest
public class ProcessTestDemo3 {
    //可以得到流程定义信息
    //注入RepositoryService
    @Autowired
    private RepositoryService repositoryService;

    //注入RuntimeService
    //流程实列
    @Autowired
    private RuntimeService runtimeService;

    //注入TaskService
    //流程任务
    @Autowired
    private TaskService taskService;

    //注入
    //历史相关操作
    @Autowired
    private HistoryService historyService;

    //1、部署流程定义和启动流程实列
    @Test
    public void deployProcess(){
        //流程部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/jiaban04.bpmn20.xml")
                .name("加班申请流程")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }
    /**
     * 启动流程实例
     */
    @Test
    public void startUpProcess03() {
        //创建流程实例,我们需要知道流程定义的key
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiaban04");
        //输出实例的相关信息
        System.out.println("流程定义id：" + processInstance.getProcessDefinitionId());
        System.out.println("流程实例id：" + processInstance.getId());
    }

    //2、查询组任务
    @Test
    public void findGroupTaskList() {
        List<Task> list = taskService.createTaskQuery()
                .taskCandidateUser("tom01")
                .list();
        for (Task task : list) {
            System.out.println("================");
            System.out.println("流程实列ID：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }
    }
    //3、拾取组任务
    @Test
    public void claimTask(){
        Task task = taskService.createTaskQuery()
                .taskCandidateUser("tom01")
                .singleResult();
        if (task != null){
            taskService.claim(task.getId(), "tom01");
            System.out.println("任务拾取了");
        }

    }

    //4 查询个人的代办任务--tom01
    @Test
    public void findTaskLIst(){
        String aggign = "tom01";
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee(aggign).list();
        for (Task task : list){
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }

    }

    //5 办理个人任务
    @Test
    public void completeTask(){
        //查询负责人处理的任务，返回一条
        Task task = taskService.createTaskQuery()
                .taskAssignee("tom01")
                .singleResult();
        //完成任务,参数是任务id
        taskService.complete(task.getId());

    }

    //6 归还组任务


    //查询已经处理的任务
    @Test
    public void findcompleteTasklist(){
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee("tom01").finished().list();
        for (HistoricTaskInstance historicTaskInstance:list){
            System.out.println("流程实例id：" + historicTaskInstance.getProcessInstanceId());
            System.out.println("任务id：" + historicTaskInstance.getId());
            System.out.println("任务负责人：" + historicTaskInstance.getAssignee());
            System.out.println("任务名称：" + historicTaskInstance.getName());
        }
    }


}

